{literal}
    <style type="text/css">
		@import url('../static/admin/css/shippingtime.css');
		
		.action .btn24{
			margin-left: 4px;
			margin-right: 4px;
			float:left;
		}
		.for_editing
		{
			display: none;
		}
		
		a.btn{
			border: 1px solid black;
			color: #1575BF;
			font-size: 14pt;
			padding: 6px;
			text-decoration: none;
		}
		a.btn:hover{			
			text-decoration: none;			
		}
		
    </style>
{/literal}

{include file = "styles/header.tpl"}
<div id="content" class="box">
    <h1>Order Status</h1>
    <form method="post" id="frmRules" name="frmRules" action="{$smarty.const.HOST_ADMIN}/shippingtime/importrule" enctype="multipart/form-data">
        <fieldset style="float:left;width:25%">					
			<div >
				<input type="file" name="rules_file" id="rules_file" style="padding:0px;font-size: 10pt;">
				<button type="submit" style="padding:3px;font-size: 10pt;">Import</button>				
			</div>			
        </fieldset>
		<fieldset style="float:right;width:70%">
			<div >				
				<a class="btn" href="{$smarty.const.HOST_ADMIN}/shippingtime/neworderstatus">New Rule</a>				
			</div>	
		</fieldset>
		<div style="clear:both"></div>		
	</form>
    <table id="rules_list" width="100%">
        <thead>
			<tr>            
				<th>Id</th>
				<th>Display Status</th>
				<th>Oms Status</th>
				<th class="last">Action</th>
			</tr>
		</thead>
		<tbody>
        {foreach  from=$orderstatus key=id_status_display item=status}			
            <tr class="status_data">                
				<td class="status_id">
                    <span class="for_editing">
                        <input type="text" class="status_id" name="status_id" value="{$id_status_display}" >
                    </span>
                    <span>{$id_status_display}</span>
                </td>                
                <td class="status_id">
                    <span class="for_editing">
                        <input type="text" name="status_text" value="{$status.text}" >
                    </span>
                    <span class="for_view">{$status.text}</span>
                </td>
                
                <td class="rule_action" style="width:300px">
                    <span class="for_editing">
                        <textarea name="rule_action">{$status.order_item_status_string}</textarea>
                    </span>
                    <span class="for_view">
                        {$status.order_item_status_string}
                    </span>					
                </td>                
                <td class="action" style="width:100px" align=center>
                    <a title="edit" class="btn24 edit" href="#"><span></span></a>
                    <a title="insert before" class="btn24 add" href="#"><span></span></a>
                    <!--<button class="btn reset"><span class=""></span></button>-->
                    <!--<button class="btn save disabled"><span class=""></span></button>-->
                    <!--<button title="insert before" class="btn add"><span class=""></span></button>                    -->
                    <button title="delete" class="btn delete"><span class=""></span></button>                    
                </td>
            </tr>
        {/foreach}
	</tbody>
    </table>
</div>

<script type="text/javascript">
    var base_url = "{$smarty.const.HOST_ADMIN}";
    {literal}
    $(function(){
		//init
		$(".for_editing").hide();		
		$(".btn.reset").hide();
		$("input#date").datepicker();
		$("input#date").datepicker("option","dateFormat","yy-mm-dd");
		
		/*
		$("#rules_list tbody").sortable({
			revert: true
		});
		$("tr.rule_data").draggable({
		  connectToSortable: "#rules_list tbody",
		  helper: "clone",
		  revert: "invalid"
		});
		*/		
		
		$("#content").delegate(".btn.edit", "click", function(){
			var tr_tag = $(this).parent().parent();
			var rule_id = tr_tag.find("input.rule_id").val();
			window.location.href = base_url+'/shippingtime/editrule/rule_id/'+rule_id;
			//$(this).hide();			
			return false;
		});
		
		$("#content").delegate(".btn.add", "click", function(){
			var tr_tag = $(this).parent().parent();
			var rule_order = tr_tag.find("input[name='rule_order']").val();
			window.location.href = base_url+'/shippingtime/newrule?order='+rule_order;
			//$(this).hide();			
			return false;
		});
		
		$("#content").delegate(".btn.reset", "click", function(){
			var tr_tag = $(this).parent().parent();
			disableRuleEdit(tr_tag);
			//$(this).hide();
			return false;
		});
		
		$("#content").delegate(".btn.save", "click",function(){
			if($(this).hasClass("disabled"))
				return false;
			var tr_tag = $(this).parent().parent();
			saveRule(tr_tag);						
			return false;
		});
		
		$("#content").delegate(".btn.delete", "click", function(){
			var tr_tag = $(this).parent().parent();			
			if(confirm("Do you want to delete this rule?") == false)
				return false;			
			deleteRule(tr_tag);						
			return false;
		});
		
		$("#content").delegate("a.btn24.up", "click", function(){
			var tr_tag = $(this).parent().parent();
			var prev_tr_tag =  tr_tag.prev();
			if(prev_tr_tag.hasClass("rule_data") == false)
				return false;
			swapRuleOrder(tr_tag, prev_tr_tag);					
			return false;
		});
		
		$("#content").delegate("a.btn24.down", "click", function(){
			var tr_tag = $(this).parent().parent();
			var next_tr_tag =  tr_tag.next();
			if(next_tr_tag.hasClass("rule_data") == false)
				return false;
			swapRuleOrder(tr_tag, next_tr_tag);					
			return false;
		});
		
		$("#test_shipping_service").click(function(){
			var data = new Object;
			data.location = $("select#location").val();
			data.sku = $("input#sku").val();
			if($("input#date").val())
				data.date = $("input#date").val();
			$("span#result").hide('fast');
			$("span#error").hide('fast');
			
			var debug = $("input.debug").attr('checked') == 'checked'? true:false;
			if(debug == true)
				data.debug = 1;
						
			var url_service = "http://localhost/lazada/shippingtime_service/get_time_shipping";
			var url_service2 = "http://ops.lazada.com/shippingtime_service/get_time_all_location?sku=AP069EL46OJDANVN-46336&format=html";
			$.get(url_service2, data, function(response){
				$("span#result").text("");
				$("span#error").text("");
				
				$("span#result").show('fast');
				$("span#error").show('fast');
			
				if(debug == true)
						console.log(response.debug);
						
				if(response.error == 1)
				{
					$("span#error").text(response.message);
					return;
				}
				
				if(response.result_type == 1)
				{
					$("span#result").text(" = "+response.data.result);					
				}
				else{
					$("span#result").text(" + "+response.data.result + '(h)');
				}								
			},'json');	
			return false;
		});
    });
	
	function enableRuleEdit(tr_tag)
	{
            tr_tag.find(".for_editing").show();
			tr_tag.find(".btn.reset").show();
            tr_tag.find(".for_view").hide();
			tr_tag.find(".btn.edit").hide();
			tr_tag.find(".btn.save").removeClass('disabled');			
			tr_tag.find("input.rule_enabled_checkbox").removeAttr('disabled');
	}
	
	function disableRuleEdit(tr_tag)
	{
            tr_tag.find(".for_editing").hide();
			tr_tag.find(".btn.reset").hide();
            tr_tag.find(".for_view").show();
			tr_tag.find(".btn.edit").show();
			tr_tag.find(".btn.save").addClass('disabled');			
			tr_tag.find("input.rule_enabled_checkbox").attr('disabled', 'disabled');
	}

	function saveRule(tr_tag)
	{		
            var data = tr_tag.find('input').serialize() + "&" + tr_tag.find('textarea').serialize();
            var url = base_url + '/shippingtime/updaterule';
            $.get(url, data, function(response){
				callbackSaveRule(tr_tag, response);				
			}, 'json');			
	}
	
	function deleteRule(tr_tag)
	{		
            var data = tr_tag.find('input.rule_id').serialize();
            var url = base_url + '/shippingtime/deleterule';
            $.get(url, data, function(response){
				callbackDeleteRule(tr_tag, response);				
			});			
	}
	
	function swapRuleOrder(tr_tag1, tr_tag2)
	{
		// swap number
		//var number1 = tr_tag1.find('span.number').text();
		//var number2 = tr_tag2.find('span.number').text();		
		//tr_tag1.find('span.number').text(number2);
		//tr_tag2.find('span.number').text(number1);
			
		var rule_id1 = tr_tag1.find('.rule_id input').val();
		var rule_id2 = tr_tag2.find('.rule_id input').val();
		var order1 = tr_tag1.find('.rule_order input').val();
		var order2 = tr_tag2.find('.rule_order input').val();	
		
		var data = new Object();
		data.rule_id1 = rule_id1;
		data.rule_id2 = rule_id2;
		data.rule_order1 = order1;
		data.rule_order2 = order2;
		
		var url = base_url + '/shippingtime/swapruleorder';
		$.get(url, data, function(response){
			
		});		
		// swap view
		var html1 = tr_tag1.html();
		var html2 = tr_tag2.html();
		
		tr_tag1.hide('fast');
		tr_tag1.html(html2);
		tr_tag1.show('slow');
		
		tr_tag2.hide('fast');		
		tr_tag2.html(html1);				
		tr_tag2.show('slow');	
		
		// swap order					
		tr_tag1.find('.rule_order input').val(order1);
		tr_tag2.find('.rule_order input').val(order2);	
	}
	
	function callbackSaveRule(tr_tag, response)
	{
		disableRuleEdit(tr_tag);	
		if(response.error == -1)
			return;			
		var rule = response.data; 
		var td_rule_comment_tab = tr_tag.find("td.rule_comment .for_view").text(rule.comment);
		var td_rule_comment_tab = tr_tag.find("td.rule_order .for_view").text(rule.order);
		var td_rule_comment_tab = tr_tag.find("td.rule_cond .for_view").html(rule.conditions_parsed);
		var td_rule_comment_tab = tr_tag.find("td.rule_action .for_view").html(rule.action_parsed);
	}
	
	function callbackDeleteRule(tr_tag, response)
	{	
		if(response != 1)
			return;		
		tr_tag.remove();
	}
	
	function parseRule()
	{
		var arr_tr_rule = $("table#rules_list").find("tr.rule_data");
		$.each(arr_tr_rule, function(key, tr_tag){			
			
			var conditions_json =  $(tr_tag).find(".rule_cond textarea").text();
			//var conditions = JSON.stringify(conditions_json);
			//console.log(conditions_json);
			var conditions = jQuery.parseJSON(conditions_json);
			//console.log(conditions);
			$.each(conditions, function(key, condition){				
				//console.log(condition);				
			});
		});
	}
    {/literal}
</script>